//
//  FinancialListViewController.m
//  HSSFinancial
//
//  Created by xinqiao on 2017/1/10.
//  Copyright © 2017年 Jon_G. All rights reserved.
//

#import "FinancialListViewController.h"

#import "FinanciaInstallmentTableViewCell.h"
#import "G_Color.h"


#import "HuisangsuoApi.h"
#import "promptAlertViewController.h"
#import "MBProgressHUD.h"
#import "MJRefresh.h"

@interface FinancialListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger page_i;
    NSInteger type_i;
}
@property (nonatomic, strong) MBProgressHUD *hud;

@property (nonatomic, strong) NSArray *hui_Plan_Array;

@end

@implementation FinancialListViewController

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = _title_name;
    self.automaticallyAdjustsScrollViewInsets = NO;
    page_i = 1;
    type_i = 1;
    [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:0];
    self.financialListTableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page_i ++;
        [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:1];
    }];
    self.financialListTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:1];
        [self.financialListTableView.header endRefreshing];
    }];
    [self setDateSourceForFinancialHuiPlanlistUI];
}
// 请求
- (void)getFinancialHuiPlanDateForPage:(NSInteger)page Type:(NSInteger)type IsRefresh:(NSInteger)isRefresh {
    
    if (isRefresh == 0) {
        self.hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        self.hud.mode = MBProgressHUDModeCustomView;
        self.hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        self.hud.bezelView.color = [UIColor clearColor];
    }
    
    NSDictionary *dic = @{
                          @"page":[NSString stringWithFormat:@"%ld",page],
                          @"type":[NSString stringWithFormat:@"%ld",type]
                          };
    
    [HuisangsuoApi requestgetDate:MainFundManage dic:dic AndBack:^(id obj) {
        if (isRefresh == 0) {
            [self.hud hideAnimated:YES];
        }
        if ([obj[@"message"] isEqualToString:@"没有更多数据了"]) {
            [self.financialListTableView.footer noticeNoMoreData];
            return ;
        }
        if (obj != nil) {
            if ([obj[@"status"] integerValue] == 1) {
                if (page == 1) {
                    self.hui_Plan_Array = obj[@"list"];
                    
                }else {
                    self.hui_Plan_Array = [self.hui_Plan_Array arrayByAddingObjectsFromArray:obj[@"list"]];
                    [self.financialListTableView.footer endRefreshing];
                }
            }else {
                [promptAlertViewController showButtonAlertView:self withMessage:obj[@"message"] actionMessage:@"确定"];
            }
        }else {
            [promptAlertViewController showButtonAlertView:self withMessage:@"暂无数据!" actionMessage:@"确定"];
        }
        
        [self.financialListTableView reloadData];
    } error:^(id obj) {
        if (isRefresh == 0) {
            [self.hud hideAnimated:YES];
        }
        [promptAlertViewController showButtonAlertView:self withMessage:@"请求错误，稍后重试！" actionMessage:@"确定"];
    }];
}
#pragma mark - UITableView
- (void)setDateSourceForFinancialHuiPlanlistUI {
    self.financialListTableView.delegate = self;
    self.financialListTableView.dataSource = self;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.hui_Plan_Array.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}
// 返回section的头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, [UIScreen mainScreen].bounds.size.width, 10)];
    sectionView.backgroundColor = [G_Color colorWithNSString:@"efeff4" alpha:1.0];
    return sectionView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 151;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellId = @"cell";
    FinanciaInstallmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"FinanciaInstallmentTableViewCell" owner:self options:nil]lastObject];
        
        cell.titleNameLabel.text = [NSString stringWithFormat:@"%@",self.hui_Plan_Array[indexPath.section][@"borrow_name"]];
        cell.borrow_basis_rateLabel.text = [NSString stringWithFormat:@"%@%%",self.hui_Plan_Array[indexPath.section][@"borrow_basis_rate"]];
        cell.borrow_durationLabel.text = [NSString stringWithFormat:@"%@",self.hui_Plan_Array[indexPath.section][@"borrow_duration"]];
        cell.transfer_total_Label.text = [NSString stringWithFormat:@"%@份",self.hui_Plan_Array[indexPath.section][@"transfer_total"]];
        cell.progress_View.progress = [self.hui_Plan_Array[indexPath.section][@"progress"] integerValue] * 0.01;
        cell.per_transfer_Label.text = [NSString stringWithFormat:@"%@元起",self.hui_Plan_Array[indexPath.section][@"per_transfer"]];
        cell.need_transfer_Label.text = [NSString stringWithFormat:@"剩余份数：%@份",self.hui_Plan_Array[indexPath.section][@"need_transfer"]];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setAccessoryType:UITableViewCellAccessoryNone];
        return cell;
    }
    
    
    
    return cell;
}
// 下划线
- (void)setButtonLineFrame:(CGFloat)x {
    [UIView animateWithDuration:0.3 animations:^{
        CGRect lineFrame = self.hui_Plan_Line.frame;
        lineFrame.origin.x = x;
        self.hui_Plan_Line.frame = lineFrame;
    }];
}
// 按钮变化
- (void)setButtonTitleColor:(UIButton *)button1 {
    for (UIView * view in self.hui_BackGroundView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            UIButton * button =(UIButton * )view;
            [button setTitleColor:[G_Color colorWithNSString:@"333333" alpha:1.0] forState:UIControlStateNormal];
        }
    }
    [button1 setTitleColor:[G_Color colorWithNSString:@"ff5050" alpha:1.0] forState:UIControlStateNormal];
    [self setButtonLineFrame:button1.frame.origin.x];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)addHuiPlanButtonAAction:(id)sender {
    [self setButtonTitleColor:self.hui_ButtonA];
    page_i = 1;
    type_i = 1;
    [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:0];
    self.financialListTableView.footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page_i ++;
        [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:1];
    }];
}
- (IBAction)addHuiPlanButtonBAction:(id)sender {
    [self setButtonTitleColor:self.hui_ButtonB];
    page_i = 1;
    type_i = 1;
    [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:0];
    self.financialListTableView.footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page_i ++;
        [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:1];
    }];
}
- (IBAction)addHuiPlanButtonCAction:(id)sender {
    [self setButtonTitleColor:self.hui_ButtonC];
    page_i = 1;
    type_i = 1;
    [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:0];
    self.financialListTableView.footer =[MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        page_i ++;
        [self getFinancialHuiPlanDateForPage:page_i Type:type_i IsRefresh:1];
    }];
}
@end
